#pragma once
#include "Face.h"
#include <QVector>

Face::Face(Face& other)
{
	clear(); 
	for(std::size_t i=0; i < other._vertexSet.size(); i++) 
		_vertexSet.push_back(other._vertexSet[i]);
	
}  
void Face::clear()
{	
	_vertexSet.clear();
	_edgeSet.clear();
	_norm.reset();
	_area=0;
} 
void Face::addTriangle(int v0, int v1, int v2)
{
	clearVertex();
	_vertexSet.push_back(v0);
	_vertexSet.push_back(v1);
	_vertexSet.push_back(v2);
}  // Face::add_triangle

void Face::replaceEdge(int old_id, int new_id)
{
	int nr_edges = _edgeSet.size();
	for(int i=0; i < nr_edges; i++) 
	{
		if (_edgeSet[i] == old_id) 
		{
			_edgeSet[i] = new_id;
			return;
		}
	}
}

void Face::replaceVertex(int old_id, int new_id)
{
	//  cout << "  Face::replace_vertex(" << old_id << ", " << new_id << ")" << endl;

	int nr_vertices = _vertexSet.size();
	for(int i=0; i < nr_vertices; i++) 
	{
		if (_vertexSet[i] == old_id) 
		{
			_vertexSet[i] = new_id;
			return;
		}
	}  // for
}  // Face::replace_vertex

int	Face::hasVertex(int v_id)
{
	int nr_vertices = _vertexSet.size();

	for(int i=0; i < nr_vertices; i++) 
	{
		if (_vertexSet[i] == v_id) return i;  // note 
	}

	return -1;

}  // Face::has_vertex

bool Face::hasEdge(int edge_index)
{
	int nr_edge = _edgeSet.size();

	for(int i=0; i < nr_edge; i++) 
	{
		if (_edgeSet[i] == edge_index) 
			return true;  
	}

	return false;
}

void Face::getOtherVertex(int index,QVector<int> &result)
{
	for(int i=0;i<_vertexSet.size();++i) {
		if(index==_vertexSet[i]) continue;
		result.push_back(_vertexSet[i]);
	}
	if(result.size()!=_vertexSet.size()-1) {
		////std::cout<<"Return vector size ERROR! @Face::get_another_vertex"<<std::endl;	
	}
	
	
}

int	Face::getThirdVertex(int v0, int v1)
{
	for(int i=0; i < 3; i++) {
		if ((_vertexSet[i] != v0) && (_vertexSet[i] != v1)) return _vertexSet[i];
	}

	return -1;

}  // Face::get_third_vertex



void Face::reverseVertices()
{
	int nr_vertices = _vertexSet.size();
	for(int i=0; i < nr_vertices/2; i++) {
		int v_id = _vertexSet[i];
		_vertexSet[i] = _vertexSet[nr_vertices - i - 1];
		_vertexSet[nr_vertices - i - 1] = v_id;
	}

}
bool Face::isEqual(Face* face)
{
	if(_vertexSet.size()!=face->getVertexNumber())
		return false;
	int equalnum=0;
	for(int i=0;i<_vertexSet.size();i++)
	{
		for(int j=0;j<face->getVertexNumber();j++)
		{
			if(_vertexSet[i]==face->getVertex(j))
			{
				equalnum++;
				break;
			}
		}
	}
	if(equalnum==_vertexSet.size())
	{
		return true;
	}
	else
	{
		return false;
	}
}

int Face::getEdge(int index)
{
	if(index<_edgeSet.size()) 
	{
		return _edgeSet[index];
	}
	else
	{
		return -1;
	}
}
int Face::hasCommonEdge(Face* face)
{
	int returnValue=0;
	for(int i=0;i<_edgeSet.size();i++)
	{
		for(int j=0;face&&j<face->getEdgeNumber();j++)
		{
			if(_edgeSet[i]==face->getEdge(j))
			{
				returnValue++;
			}
		}
	}
	return returnValue;
}


